package com.group2.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group2.equipment.entity.AddressVO;
import com.group2.equipment.entity.EquipmentInfo;
import com.group2.equipment.feign.AddressFeign;
import com.group2.equipment.mapper.EquipmentInfoMapper;
import com.group2.equipment.service.EquipmentInfoService;
import com.group2.equipment.utils.PageRequest;
import com.group2.equipment.utils.WEBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author binghan
 * @version 1.0
 * @description: TODO 设备信息业务类
 * @date 2022/6/6 16:48
 */
@Service
public class EquipmentInfoServiceImpl extends ServiceImpl<EquipmentInfoMapper, EquipmentInfo> implements EquipmentInfoService {

    @Autowired
    AddressFeign addressFeign;

    @Override
    public PageInfo<EquipmentInfo> queryPageByCondition(PageRequest<EquipmentInfo> req) {

        // 获取所有的地址信息
        List<AddressVO> addressVOS = addressFeign.selectAll();

        // 地址信息转换 map
        // key：地址id value：地址名称
        Map<Integer, String> addressMap = addressVOS.stream().collect(Collectors.toMap(AddressVO::getAddressId, AddressVO::getAddressName));
        // 获取请求参数
        EquipmentInfo equipmentInfo = req.getData();
        System.out.println(req.getData());
        // 创建查询对象
        QueryWrapper<EquipmentInfo> equipmentInfoQueryWrapper = new QueryWrapper<>();
        // 请求参数不为空
        if(!WEBUtil.isEmpty(equipmentInfo)) {
            // 封装查询条件
            // id 等值查询 三个参数 第一个条件是否满足 满足则拼接sql
            equipmentInfoQueryWrapper.eq(!WEBUtil.isEmpty(equipmentInfo.getEquipmentId()),
                    "equipment_id",equipmentInfo.getEquipmentId());
            // 设备名 模糊查询
            equipmentInfoQueryWrapper.like(!WEBUtil.isEmpty(equipmentInfo.getEquipmentName()),
                    "equipment_name",equipmentInfo.getEquipmentName());
            // 设备类型 等值查询
            equipmentInfoQueryWrapper.eq(!WEBUtil.isEmpty(equipmentInfo.getEquipmentType()),
                    "equipment_type",equipmentInfo.getEquipmentType());

            //报警类型 等值查询
            equipmentInfoQueryWrapper.eq(!WEBUtil.isEmpty(equipmentInfo.getAlarmType()),
                    "alarm_type",equipmentInfo.getAlarmType());

            // 连接方式 等值查询
            equipmentInfoQueryWrapper.eq(!WEBUtil.isEmpty(equipmentInfo.getLinkType()),
                    "link_type",equipmentInfo.getLinkType());

            // 连接port 模糊查询
            equipmentInfoQueryWrapper.like(!WEBUtil.isEmpty(equipmentInfo.getLinkPort()),
                    "link_port",equipmentInfo.getLinkPort());

            // 设备地址ID 等值查询
            equipmentInfoQueryWrapper.eq(!WEBUtil.isEmpty(equipmentInfo.getAddressId()),
                    "address_id",equipmentInfo.getAddressId());

        }
        // 封装分页信息 页号、页数
        // 设定分页 Num 和 Size
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
//        Page<EquipmentInfo> equipmentInfoPage = new Page<>(req.getPageNum(),req.getPageSize());

        // 返回查询结果
        List<EquipmentInfo> resList = this.list(equipmentInfoQueryWrapper);

        // 增加addressName
        for (EquipmentInfo info : resList) {
            // 通过id获取name
            String addressName = addressMap.get(info.getAddressId());
            // 设置name
            info.setAddressName(addressName);
        }

        // 返回带分页查询结果
        return new PageInfo<>(resList);
    }

    @Override
    public boolean deleteEquimentInfo(List<Integer> idList) {
        if(!WEBUtil.isEmpty(idList)) {
            // 删除集合中的id
            boolean b = this.removeByIds(idList);
            if(b) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
}
